package com.liaogui.算法题;

import java.util.*;

/**
 * @author liaogui
 * @date 2025/4/1 17:33
 */
public class 按身高排序 {
    public static void main(String[] args) {
        String[] names = {"Alice", "Bob", "Cindy", "David", "Emma"};
        int[] heights = {180, 170, 165, 185, 160};
        String[] sortedNames = sortPeople(names, heights);
        for (String name : sortedNames) {
            System.out.println(name);
        }
        sortPeople1(names, heights);
    }

    public static String[] sortPeople(String[] names, int[] heights) {
        String[] people = new String[names.length];
        Map<Integer, String> treeMap = new TreeMap<>(Comparator.reverseOrder());
        for (int i = 0; i < heights.length; i++) {
            treeMap.put(Integer.valueOf(heights[i]), names[i]);
        }
        int j = 0;
        for (Map.Entry<Integer, String> entry : treeMap.entrySet()) {
            people[j++] = entry.getValue();
        }
        return people;
    }

    public static void sortPeople1(String[] names, int[] heights) {

        Map<Integer,String> map = new HashMap<>();
        for (int i = 0; i < heights.length; i++) {
            map.put(heights[i],names[i]);
        }
        TreeSet<Integer> integers = new TreeSet<>();
        for (int i = 0; i < heights.length; i++) {
            integers.add(heights[i]);
        }
        integers.descendingIterator().forEachRemaining(el -> {
            String s = map.get(el);
            System.out.println(s);
        });
    }
}
